package cn.holy.app.xcddt.net


import cn.holy.app.xcddt.cons.AppUrl
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory


/**
 * Created by xionghao on 2018/2/23.
 */

object ApiClient {
    private val okHttpClient = OkHttpClient()
    val webService: Webservice
        get() = getRetrofit(AppUrl.BASE_URL).create(Webservice::class.java)

    private fun getRetrofit(url: String): Retrofit {
        val logInterceptor = HttpLoggingInterceptor()
        logInterceptor.level = HttpLoggingInterceptor.Level.BODY

        val client = okHttpClient.newBuilder()
                .addInterceptor(TokenInterceptor())
                .addInterceptor(logInterceptor)
                .build()

        val gson = GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create()
        return Retrofit.Builder()
                .client(client)
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build()
    }
}
